import axios from "axios";
import Vue from "vue";
import router from "../router";

const http = axios.create({
  baseURL: "http://192.168.101.13:10001/api"
  // baseURL: process.env.VUE_APP_API_URL || "/admin/api",
});

http.interceptors.request.use(
  config => {
    if (localStorage.token) {
      config.headers["TOKEN"] = localStorage.token; //请求头加上token
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截
http.interceptors.response.use(
  res => {
    return res;
  },
  err => {
    if (err.response.data.message) {
      Vue.prototype.$message({
        type: "error",
        message: err.response.data.message //这是后台re.send({message:''})里面的数据
      });
    }
    if (err.response.status == 401) {
      router.push("/login");
    }
    return Promise.reject(err);
  }
);

export default http;
